package com.eling.elcms.system.service.impl;

import com.eling.elcms.core.service.IGenericModelService;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.system.dao.ISysParameterDao;
import com.eling.elcms.system.model.SysParameter;
import com.eling.elcms.system.service.ISysParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysParameterImpl extends GenericManagerImpl<SysParameter, Long>
		implements ISysParameter {
	private DefaultConversionService cs = new DefaultConversionService();
	
	ISysParameterDao sysParameterDao;
	
	@Autowired
	private IGenericModelService gms;
	
	@Autowired
	public void setDao(ISysParameterDao dao) {
		this.dao = dao;
		this.sysParameterDao = dao;
	}

	@Override
	public <T> T getParameter(String code) {
		SysParameter cond = new SysParameter();
		cond.setCode(code);
		List<SysParameter> list = gms.query(cond, SysParameter.class);
		if (list.isEmpty()) {
			return null;
		} else {
			SysParameter p = list.get(0);
			if (null == p.getValue()) {
				return null;
			}
			@SuppressWarnings("unchecked")
			T value = (T) cs.convert(p.getValue(), p.getValueType());
			return value;
		}
	}

}
